#include "stdafx.h"
#include "wf_snapintersectionmanual.h"

#include "pf_information.h"
#include "pf_snapper.h"


/**
 * @param both Trim both entities.
 */
WF_SnapIntersectionManual::WF_SnapIntersectionManual(
    PF_Container& container,
    PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Trim Entity",
                           container, graphicView) {

    entity2 = NULL;
    entity1 = NULL;
    coord = PF_Vector(false);
}


QAction* WF_SnapIntersectionManual::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	//tr("Intersection Manually")
    QAction* action = new QAction(tr("I&ntersection Manually"), NULL);
    //action->zetStatusTip(tr("Snap to intersection points manually"));
	action->setIcon(QIcon(":/extui/snapintersectionm.png"));	
    return action;
}


void WF_SnapIntersectionManual::init(int status) {
    PF_ActionInterface::init(status);

    snapMode = LX_Define::SnapFree;
    snapRes = LX_Define::RestrictNothing;
}



void WF_SnapIntersectionManual::trigger() {

    PF_DEBUG->print("PF_ActionSnapIntersectionManual::trigger()");

    if (entity2!=NULL && entity2->isAtomic() &&
            entity1!=NULL && entity1->isAtomic()) {

        PF_VectorSolutions sol =
            PF_Information::getIntersection(entity1, entity2, false);

        entity2 = NULL;
        entity1 = NULL;
        if (predecessor!=NULL) {
            PF_Vector ip = sol.getClosest(coord);

            if (ip.valid) {
                PF_CoordinateEvent e(ip);
                predecessor->coordinateEvent(&e);
            }
        }
        finish();
    }
}



void WF_SnapIntersectionManual::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionSnapIntersectionManual::mouseMoveEvent begin");

    PF_Entity* se = catchEntity(e);
    PF_Vector mouse = graphicView->toGraph(e->x(), e->y());

    switch (getStatus()) {
    case ChooseEntity1:
        entity1 = se;
        break;

    case ChooseEntity2: {
            entity2 = se;
            coord = mouse;

            PF_VectorSolutions sol =
                PF_Information::getIntersection(entity1, entity2, false);

            //for (int i=0; i<sol.getNumber(); i++) {
            //    ip = sol.get(i);
            //    break;
            //}

            PF_Vector ip = sol.getClosest(coord);

            if (ip.valid) {
                deletePreview();
                preview->addEntity(
                    new PF_Circle(preview,
                                  PF_CircleAttribute(
                                      ip,
                                      graphicView->toGraphDX(4))));
                drawPreview();

                PF_DIALOGFACTORY->updateCoordinateWidget(ip,
                        ip - graphicView->getRelativeZero());

            }
        }
        break;

    default:
        break;
    }

    PF_DEBUG->print("PF_ActionSnapIntersectionManual::mouseMoveEvent end");
}



void WF_SnapIntersectionManual::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {

        PF_Vector mouse = graphicView->toGraph(e->x(), e->y());
        PF_Entity* se = catchEntity(e);

        switch (getStatus()) {
        case ChooseEntity1:
            entity1 = se;
            if (entity1!=NULL && entity1->isAtomic()) {
                setStatus(ChooseEntity2);
            }
            break;

        case ChooseEntity2:
            entity2 = se;
            coord = mouse;
            if (entity2!=NULL && entity2->isAtomic() && coord.valid) {
                trigger();
            }
            break;

        default:
            break;
        }
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_SnapIntersectionManual::updateMouseButtonHints() {
    switch (getStatus()) {
    case ChooseEntity1:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Select first entity"),
                                            tr("Back"));
        break;
    case ChooseEntity2:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Select second entity"),
                                            tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_SnapIntersectionManual::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_SnapIntersectionManual::updateToolBar() {
    PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
}


// EOF
